// pages/verifyPhone/verifyPhone.js
var request = require("../../utils/request.js")

Page({

  /**
   * 页面的初始数据
   */
  data: {
    phone: '',
    userInfo: {},
    verifText: '获取验证码',
    isAgree: false, //是否同意协议
    isVerif: true, //发送验证码按钮是否可点击
    mobile: '', //手机号
    verification: '', //验证码
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.toastDialog = this.selectComponent("#toastDialog"); //此处id和页面的 wxml中的id一致

    wx.showShareMenu({ menus: ['shareAppMessage', 'shareTimeline']})
  },
/**
 * 用户点击右上角分享
 */
onShareAppMessage (res) {
  return {
    title: '迪佳极视',
    path: 'pages/index/index',
    // imageUrl:'这个是显示的图片，不写就默认当前页面的截图',
    success: function (shareTickets) {
      console.info(shareTickets + '成功');
      // 转发成功
    },
    fail: function (res) {
      console.log(res + '失败');
      // 转发失败
    },
    complete:function(res){
      // 不管成功失败都会执行
    }
  }
},

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  //获取输入的手机号
  getMobileNumber: function (e) {
    // let num = e.detail.value
    // const regex = /^(13[0-9]|14[01456879]|15[0-35-9]|16[2567]|17[0-8]|18[0-9]|19[0-35-9])\d{8}$/
    // console.log(num.length)
    // console.log(regex.test(num))
    // if (num == '') {
    //   this.setData({
    //     isVerif: false
    //   })
    //   return false
    // }
    // if (!regex.test(num) || num.length != 11) {
    //   this.toastDialog.showDialog({
    //     code: '1',
    //     message: "手机号码格式错误",
    //     timeout: true
    //   });
    //   this.setData({
    //     isVerif: false
    //   })
    //   return false
    // }
    this.setData({
      mobile: e.detail.value
    })
  },
  //获取验证码接口
  getVerif: function () {
    var that = this;
    const regex = /^(13[0-9]|14[01456879]|15[0-35-9]|16[2567]|17[0-8]|18[0-9]|19[0-35-9])\d{8}$/
    console.log(that.data.mobile)
    if (!regex.test(that.data.mobile) || that.data.mobile.length != 11) {
      that.toastDialog.showDialog({
        code: '1',
        message: "手机号格式错误",
        timeout: true
      });
      return false
    }
    // that.toastDialog.showDialog({ code: 'loading', message: '验证码', timeout: false }); //loading 加载框
    // if (!that.data.isVerif) {
    //   return false
    // }
    if (that.data.verifText != '获取验证码') {
      return false
    }
    request.postData('app/createCode', {
      mobile: this.data.mobile
    }, function (res) {

      // that.toastDialog.hideDialog(); //关闭loading提示框
      if (res.code == 0) {
        that.setData({
          testList: res.testList,
          isVerif: true
        })
        that.toastDialog.showDialog({
          code: 'warinning', message: '短信验证码已发送到您的手机，请注意查收！', totastType: 'black', timeout: true
        });
        let time = 60;
        let timer = setInterval(() => {
          if (time > 0) {
            time--;
            that.setData({
              verifText: time + 'S'
            })
          } else {
            that.setData({
              isVerif: false,
              verifText: '获取验证码'
            })
            clearInterval(timer)
          }
        }, 1000);
      } else {
        that.toastDialog.showDialog({
          code: res.code,
          message: res.message,
          timeout: true
        });
      }
    }, function (res) {
      that.toastDialog.showDialog({
        code: res.code,
        message: res.message,
        timeout: true
      });
    });
  },
  //验证码
  getVerifNum: function (e) {
    this.setData({
      verification: e.detail.value
    })
  },
  //登录接口
  gotoLogin: function () {
    var that = this
    // that.toastDialog.showDialog({ code: 'loading', message: '加载中', timeout: false }); //loading 加载框

    request.postData('app/validCode', { mobile: that.data.mobile, code: that.data.verification }, function (res) {
      // that.toastDialog.hideDialog(); //关闭loading提示框
      if (res.code == 0) {
        wx.switchTab({
          url: '../index/index'
        });
      } else {
        that.toastDialog.showDialog({ code: res.code, message: res.message, timeout: true });
      }

    }, function (res) {
      that.setData({ loading: false })
      that.toastDialog.showDialog({ code: res.code, message: res.message, timeout: true });

    });
  }
})